/*
* Copyright (c) 2009, jclarke
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * - Redistributions of source code must retain the above copyright notice,
 *   this list of conditions and the following disclaimer.
 * - Redistributions in binary form must reproduce the above copyright notice,
 *   this list of conditions and the following disclaimer in the documentation
 *   and/or other materials provided with the distribution.
 * - Neither the name of Mauricio Aguilar O. nor the names of its contributors
 *   may be used to endorse or promote products derived from this software
 *   without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 * @author: jclarke
 * Date   : May 31 , 2009
 *
 */

package org.transFX.scene.control;

import javafx.stage.Stage;
import javafx.scene.Scene;

import javafx.scene.CustomNode;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.paint.Color;
import javafx.scene.paint.Paint;
import javafx.scene.text.Text;
import javafx.scene.text.Font;
import javafx.scene.layout.Resizable;
import javafx.scene.shape.Rectangle;
import javafx.scene.shape.Ellipse;
import javafx.scene.effect.GaussianBlur;
import javafx.scene.text.TextOrigin;

import java.lang.Math;
import javafx.animation.Timeline;
import javafx.animation.KeyFrame;
import javafx.animation.Interpolator;



/**
 * @author jclarke
 */

public class ProgressBar extends CustomNode, Resizable {
    public var min : Number;
    public var max : Number = 100.0;
    public var value: Number on replace
    {
        if(value==0)
        {
            blurAnim.stop();
        }
        else if(not blurAnim.running)
        {
            blurAnim.play();
        }

    }

    public var background: Paint = Color.rgb(118,149,178);
    public var foreground: Paint = Color.rgb(216,219,225);
    public var font = bind Font{
        name: "Courier Bold Italic"
        size: height - borderWidth * 8};
    public var arcWidth:Number = 20;
    public var arcHeight: Number = 50;
    public var borderStroke: Color = Color.BLACK;
    public var borderWidth: Number = 1;
    public var fontPaint: Paint = Color.LIGHTGRAY;
    public var showPercentage = true;

    var progressText: Text;
    var backgroundText: Text;
    var progessBar: Rectangle;
    var range = bind Math.abs(max - min);
    var percentComplete: Number = bind if(max != 0) { (
                                            value - min) / max
                                        } else {0.0;
                                        };
    var currentX = bind percentComplete * width;
    var blurX:Number = 30;
    var progressString:String = bind "%%{%.0f (percentComplete*100)}";
    var progressValue = bind "{%.2f value}";
    var progressHeight = bind height - borderWidth * 4;


    var blurAnim = Timeline {
        repeatCount: Timeline.INDEFINITE
        autoReverse: true
        keyFrames: [
            at(0s) { blurX => 0.0 }
            at(2s) { blurX => currentX - progressHeight/2 tween Interpolator.EASEBOTH }
        ]
    };

    var playing = bind visible on replace {
        if(playing and value != 0) {
            blurAnim.play();
        }else {
            blurAnim.stop();
        }
    }



    public override function create(): Node {
        return Group {
            content: [
                Rectangle { //background
                    width: bind width
                    height: bind height
                    fill: bind background
                    stroke: bind borderStroke
                    strokeWidth: bind borderWidth
                    arcWidth: bind arcWidth
                    arcHeight: bind arcHeight
                    cache: true
                },
                backgroundText = Text { // Text over Background
                    translateX: bind (width - backgroundText.layoutBounds.width)/2
                    translateY: bind (height - backgroundText.layoutBounds.height)/2
                    textOrigin: TextOrigin.TOP
                    font: bind font
                    fill: bind foreground
                    content: bind if(showPercentage) progressString else progressValue
                    cache: true
                },
                Group {
                    content: [
                        progessBar = Rectangle { // Progress
                            translateX: bind borderWidth
                            translateY: bind borderWidth*2
                            width: bind currentX
                            height: bind progressHeight
                            fill: bind foreground
                            arcWidth: bind arcWidth
                            arcHeight: bind arcHeight
                        },
                        Ellipse {
                            centerX: bind blurX
                            centerY:  bind height/2
                            radiusX: bind progressHeight
                            radiusY: bind progressHeight/2
                            fill: Color.rgb(255,255,255,.7)
                            effect: GaussianBlur{ radius: 30 }
                        },
                        Text { // Text Over Progress Bar, this has to be in progressBar coordinate space
                            x: bind (width - backgroundText.layoutBounds.width)/2
                            y: bind (height - backgroundText.layoutBounds.height)/2
                            textOrigin: TextOrigin.TOP
                            font: bind font
                            fill: bind background
                            content: bind if(showPercentage) progressString else progressValue
                            clip: bind progessBar
                         }
                    ]
               }
            ]

        };
    }
}

function run(args:String[]):Void {

    var progress = 0;
    var stage:Stage = Stage {
    	title: "Progress Bar"
        scene: Scene {
            width: 920
            height: 60
            content: ProgressBar {
                value: bind progress;
            	translateX: 10
                translateY:10
                width: 890
                height: 35
                value: 50
            }
        }
    }
    Timeline {
        repeatCount: Timeline.INDEFINITE
        keyFrames : [
            KeyFrame {
                time : 1s
                canSkip : true
                action: function():Void
                {
                    progress++;
                }


            }
        ]
    }.play();


}